//
//  Utils.cpp
//  FuseSQLite
//
//  Created by Admin on 6/8/14.
//  Copyright (c) 2014 Admin. All rights reserved.
//

#include "Utils.h"

#include <string.h>

void Utils::splitPathIntoComponents(const char* path, std::vector<std::string>* components)
{
    components->clear();
    
    if (*path == '/')
        components->push_back("/");
    
    const char* pathPos = path;
    
    while (true)
    {
        const char* componentBegin = std::strstr(pathPos, "/");
        if (componentBegin == nullptr)
        {
            components->push_back(pathPos);
            return;
        }
        
        const char* componentEnd = std::strstr(componentBegin + 1, "/");
        if (componentEnd == nullptr)
        {
            std::string component(componentBegin + 1);
            if (component != "")
                components->push_back(component);
         
            return;
        }
        
        std::string component(componentBegin + 1, componentEnd);
        if (component != "")
            components->push_back(component);
        
        pathPos = componentBegin + 1;
    }
}